package com.achen.controller;

import com.achen.constant.MessageConstant;
import com.achen.entity.PageResult;
import com.achen.entity.QueryPageBean;
import com.achen.entity.Result;
import com.achen.pojo.CheckGroup;
import com.achen.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 *
 * @author achen
 * @create 2020-04-08 16:21
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //添加检查组
    @RequestMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.addCheckGroup(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL); //添加失败
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //检查组分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        return pageResult;
    }

    //根据检查组id回显信息
    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup); //查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL); //查询失败
        }
    }

    //根据检查组id查询关联表中检查项，回显复选中的复选框
    @RequestMapping("/findCheckItemByCheckGroupId")
    public Result findCheckItemByCheckGroupId(Integer id) {
        try {
            List<Integer> list = checkGroupService.findCheckItemByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list); //查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL); //查询失败
        }
    }

    //修改检查组
    @RequestMapping("/editCheckGroup")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.editCheckGroup(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL); //添加失败
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //根据检查组id查询关联表中检查项，回显复选中的复选框
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup() {
        try {
            List<CheckGroup> list = checkGroupService.findAllCheckGroup();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list); //查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL); //查询失败
        }
    }
}
